package com.fingard.dsp.bank.directbank.icbc02.receiptpro;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptData;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptDataHelper;
import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.directbank.icbc02.ICBC02ReceiptPro;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.dsp.bank.sepro.ReceiptPro;
import com.fingard.dsp.task.command.SyncReceipt;
import com.fingard.generalobj.ExecResponse;
import com.fingard.io.FileCheckHelper;
import com.fingard.io.FileHelper;
import com.fingard.net.FtpReceiver;
import com.fingard.net.MixFtp;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.ZipHelper;
import com.fingard.xml.XmlTextReader;

public class ICBC02ReceiptProFtp extends ICBC02ReceiptPro {

    /**
     * ftp方式，工行每日上传，一个日期一个zip压缩包，
     * 解压之后，每个账号一个单独目录，
     * 有html索引文件，指明各回单编号对应的pdf文件，
     * 如果每个pdf对应一个xml数据文件，则同时解析xml文件，从xml中提取该笔回单数据
     */
    @Override
    public RetAtsReceipt qryReceiptOneDay(ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, Date pOneDay) throws Exception {

        String busDate = Format.Date8Format.format(pOneDay);
        //按月建目录存下载的压缩文件
        String saveDirPath = pConfig.getSaveSubDir(pReqReceipt.reqHeader, pOneDay);
        //执行删除临时文件
        //String delLog = ReceiptPro.deleteTmpFile(saveDirPath);
        //if(StringHelper.hasAnyChar(delLog)){
        //	WriteBankLogLn("删除临时文件：\r\n" + delLog);
        //}

        String localDirPath = saveDirPath + File.separator + busDate;
        String localZipFile = saveDirPath + File.separator + busDate + ".zip";
        boolean isSucc = false;
        if (FileHelper.existFile(localDirPath)) {
            WriteBankLogLn("本地已经存在当日回单文件解压目录" + localDirPath + "，不需再下载。\r\n如果要重新下载回单文件，需删除该目录，\r\n再删除文件（如果不存在则忽略）" + localZipFile + "。");
            isSucc = true;
        } else {
            //工行新银企电子回单主动推送，无html文件
            boolean newReceiptPush = getBankConfig().getValueAsBool("newReceiptPush");
            if (newReceiptPush) {
                return downZipFileByNewReceiptPush(busDate, pReqReceipt.actNum, pConfig, pReqReceipt, localDirPath, pOneDay);
            }

            ExecResponse resp = downZipFile(busDate, pReqReceipt.actNum, localDirPath, localZipFile);
            if (resp.isSucceed) {
                //有zip文件，并且已经解压，后面取解压后的目录
                isSucc = true;
            } else {
                WriteBankLogLn("回单文件下载失败：" + resp.logMsg);
            }
        }
        if (isSucc) {
            if (pConfig.deleteAfterUploaded) {
                boolean delResult = FileHelper.deleteFileIfExists(localZipFile);
                if (delResult) {
                    WriteBankLogLn("已删除下载的压缩文件：" + localZipFile);
                }
            }
            String relativeSaveDir = pConfig.getRelativeSaveDir(pReqReceipt.reqHeader, pOneDay);
            relativeSaveDir += File.separator + busDate;
            ExecResponse resp2 = getPdfFiles(pReqReceipt, pOneDay, localDirPath, relativeSaveDir);
            WriteBankLogLn("提取pdf文件结果：" + resp2.logMsg);
        } else {
            WriteBankLogLn("账号" + pReqReceipt.actNum + "，日期" + Format.Date10Format.format(pOneDay) + "，结束。");
        }
        return null;

    }

    /**
     * 新银企电子回单主动推送
     * 命名格式为groupid_日期_1_1.zip，其中最后的1_1表示本次推送共1个zip包，
     * 本zip文件为第1个。如果当天共推送3个zip文件，
     * 则其中第一个zip文件命名为：groupid_日期_3_1.zip。
     * zip解压缩后为电子回单的pdf文件，每个zip包最多200个pdf
     * 境内账号：账号_日期YYYYMMDD_流水号_地区号#网点号#柜员号#主机交易流水号_生成的顺序号.pdf
     * 境外账号：账号_日期YYYYMMDD_流水号_地区号#协议编号#子协议序号#交易发生日期YYYY-MM-DD#明细序号_生成的顺序号.pdf
     *
     * @param busDate
     * @param actNum
     * @param pConfig
     * @param pReqReceipt
     * @param localDirPath
     * @param pOneDay
     * @return
     */
    private RetAtsReceipt downZipFileByNewReceiptPush(String busDate, String actNum, ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, String localDirPath, Date pOneDay) throws Exception {
        boolean isSucc = false;
        //本地回单目录
        String localpath = pConfig.getSaveSubDir(pReqReceipt.reqHeader, Format.Date8Format.parse(busDate)) + File.separator + pReqReceipt.actNum + File.separator + busDate;
        //创建MixFtp对象
        FtpSet ftpSet = bankFront.getFtpSet("receipt");
        MixFtp mixFtp = new MixFtp(ftpSet);
        //获取需要下载的压缩包列表
        List<String> downList = getDownloadZipFileList(mixFtp, busDate);
        //开始下载到本地临时目录
        for (String zipFile : downList) {
            isSucc = false;
            ExecResponse resp = downZipFile(busDate, localDirPath, localDirPath + zipFile);
            if (resp.isSucceed) {
                //有zip文件，并且已经解压，后面取解压后的目录
                isSucc = true;
            } else {
                WriteBankLogLn(zipFile + "回单文件下载失败：" + resp.logMsg);
            }
            if (isSucc) {
                if (pConfig.deleteAfterUploaded) {
//                    boolean delResult = FileHelper.deleteFileIfExists(localZipFile);
//                    if (delResult) {
//                        WriteBankLogLn("已删除下载的压缩文件：" + localZipFile);
//                    }
                }
                String relativeSaveDir = pConfig.getRelativeSaveDir(pReqReceipt.reqHeader, pOneDay);
                relativeSaveDir += File.separator + busDate;
                ExecResponse resp2 = getPdfFilesWithoutHtml(pReqReceipt, pOneDay, localDirPath, relativeSaveDir);
                WriteBankLogLn("提取pdf文件结果：" + resp2.logMsg);
            } else {
                WriteBankLogLn("账号" + pReqReceipt.actNum + "，日期" + Format.Date10Format.format(pOneDay) + "，结束。");
            }
        }
        return null;
    }

    private ExecResponse getPdfFilesWithoutHtml(ReqQryReceipt pReqReceipt, Date pOneDay, String pReceiptDir, String relativeSaveDir) throws Exception {
        ExecResponse resp = new ExecResponse();
        File rDir = new File(pReceiptDir);
        if (!rDir.exists()) {
            resp.logMsg = "目录" + pReceiptDir + "不存在";
            FGBiz.logTrace.error(this.getLogCode(), resp.logMsg);
            return resp;
        }
        String charset = this.getCharset("receipt");
        String charsetXmlFile = this.getCharset("receiptXmlFile");
        File[] receiptFiles = rDir.listFiles();
        ReceiptDataHelper receiptHelper = null;//回单数据保存类，分区分表
        try {
            receiptHelper = new ReceiptDataHelper(pReqReceipt.reqHeader.tenant, pOneDay);
            int sCount = 0;
            int existCount = 0;
            int totalCount = 0;
            String aLog = "";
            String actNum = pReqReceipt.actNum.toLowerCase();
            for (File receiptFile : receiptFiles) {
                String eachName = receiptFile.getName().toLowerCase();
                if (eachName.contains(actNum)){
                    aLog = "解析" + receiptFile.getName();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (receiptHelper != null) {
                receiptHelper.close();
            }
        }

        String actNum = pReqReceipt.actNum.toLowerCase();
        return null;
    }

    private ExecResponse downZipFile(String busDate, String pLocalDirPath, String pLocalZipFile) throws IOException {
        ExecResponse resp = new ExecResponse();
        FtpSet ftpSet = bankFront.getFtpSet("receipt");
        MixFtp mixFtp = null;
        String timePostfix = ReceiptPro.getTransID();
        String localTmpFilePath = pLocalZipFile + "." + timePostfix + ".tmp";
        String tmpDecompressDir = pLocalZipFile + "." + timePostfix + ".unzip";
        FtpReceiver tmpReceiver = null;
        String downType = bankFront.getStrValueByFor("receiptDownloadType", "");
        FGBiz.logTrace.debug(this.getLogCode(), "receiptDownloadType:" + downType);

        try {
            mixFtp = new MixFtp(ftpSet);
            String ftpFileName = busDate + ".zip";
            WriteBankLogLn("FTP下载地址: [" + ftpSet.serverIP + ":" + ftpSet.port + "].");
            WriteBankLogLn("开始下载服务器文件- [目录" + ftpSet.downDir + "文件" + ftpFileName + "],至本地临时文件- [" + localTmpFilePath + "].");

            tmpReceiver = mixFtp.downloadFileFromDir(ftpSet.downDir, ftpFileName, localTmpFilePath);
            if (StringHelper.hasAnyChar(tmpReceiver.connMsg)) {
                WriteBankLogLn(tmpReceiver.connMsg);
            }

            File depressDir = new File(tmpDecompressDir);
            if (tmpReceiver.isCompleted) {
                if (!depressDir.exists()) {
                    depressDir.mkdirs();
                }
                WriteBankLogLn("解压至：[" + tmpDecompressDir + "]");
                ZipHelper.unzipFile(localTmpFilePath, tmpDecompressDir);
                WriteBankLogLn("执行解压结束");
                if (depressDir.exists()) {
                    File[] deFiles = depressDir.listFiles();
                    if (deFiles != null && deFiles.length > 0) {
                        boolean isHtmlChked = false;
                        boolean isPdfChked = false;
                        //校验通过后，需重命名的
                        File needRenameDir = depressDir;

                        if (isHtmlChked == false) {
                            resp.logMsg = "解压后文件夹校验不通过，未找到html文件\r\n";
                        }
                        if (isPdfChked == false) {
                            resp.logMsg += "解压后文件夹校验不通过，未找到pdf文件\r\n";
                        }
                        //文件校验通过
                        if (isHtmlChked && isPdfChked) {
                            WriteBankLogLn(needRenameDir.getAbsolutePath() + "重命名为" + pLocalDirPath);
                            File destDir = new File(pLocalDirPath);
                            boolean renamed = needRenameDir.renameTo(destDir);
                            //重命名目录成功
                            if (renamed && destDir.exists()) {
                                //重命名zip文件
                                WriteBankLogLn(localTmpFilePath + "重命名为" + pLocalZipFile);
                                File tmpZip = new File(localTmpFilePath);
                                File destZip = new File(pLocalZipFile);
                                boolean rezip = tmpZip.renameTo(destZip);
                                if (rezip && destZip.exists()) {
                                    resp.isSucceed = true;
                                } else {
                                    resp.logMsg = "重命名失败：" + localTmpFilePath + "重命名为" + pLocalZipFile;
                                }
                            } else {
                                resp.logMsg = "重命名失败：" + needRenameDir.getName() + "重命名为" + pLocalDirPath;
                            }
                        }
                    } else {
                        resp.logMsg = "解压后文件夹" + tmpDecompressDir + "下为空";
                    }
                } else {
                    resp.logMsg = "解压目标文件夹" + tmpDecompressDir + "不存在，创建失败";
                }
            } else {
                resp.logMsg = tmpReceiver.connMsg;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (mixFtp != null) {
                mixFtp.disconnect();
            }
            try {
                WriteBankLogLn("删除临时目录：" + tmpDecompressDir);
                FileHelper.deleteRecursive(tmpDecompressDir);
            } catch (Exception e) {
                WriteBankLogLn(e);
            }
            try {
                WriteBankLogLn("删除临时文件：" + localTmpFilePath);
                FileHelper.deleteFileIfExists(localTmpFilePath);
            } catch (Exception e) {
                WriteBankLogLn(e);
            }
        }

        return resp;
    }

    private List<String> getDownloadZipFileList(MixFtp mixFtp, String busDate) throws Exception {
        //电子回单会在客户路径下保留三天，遍历回单下载目录，找到需要下载的压缩文件
        ArrayList<String> fileList = mixFtp.listNames(mixFtp.ftpSet.downDir);
        if (fileList.size() == 0) {
            throw new RuntimeException(mixFtp.ftpSet.downDir + "目录为空");
        }
        String receiptGroupId = getBankConfig().getValueAsString("receiptGroupId");
        if (StringHelper.isNullOrEmpty(receiptGroupId)) {
            throw new RuntimeException("receiptGroupId不得为空");
        }
        for (int i = 0; i < fileList.size(); i++) {
            //groupid_日期_1_1.zip
            String[] zipFileArr = fileList.get(i).split("_");
            if (zipFileArr.length != 4) {
                fileList.remove(i);
                continue;
            }
            if (!(receiptGroupId.equals(zipFileArr[0]) && busDate.equals(zipFileArr[1]))) {
                fileList.remove(i);
                continue;
            }
        }
        return fileList;
    }

    /**
     * 下载的日期目录中提取该账号对应的html，再提取pdf
     *
     * @param pRelativeSaveDir 相对文件夹目录
     */
    private ExecResponse getPdfFiles(ReqQryReceipt pReqReceipt, Date pOneDay, String pReceiptDir, String pRelativeSaveDir) {
        ExecResponse resp = new ExecResponse();
        File rDir = new File(pReceiptDir);
        if (!rDir.exists()) {
            resp.logMsg = "目录" + pReceiptDir + "不存在";
            FGBiz.logTrace.error(this.getLogCode(), resp.logMsg);
            return resp;
        }
        String charset = this.getCharset("receipt");
        String charsetXmlFile = this.getCharset("receiptXmlFile");
        File[] receiptFiles = rDir.listFiles();
        ReceiptDataHelper receiptHelper = null;//回单数据保存类，分区分表
        try {
            receiptHelper = new ReceiptDataHelper(pReqReceipt.reqHeader.tenant, pOneDay);
            int sCount = 0;
            int existCount = 0;
            int totalCount = 0;
            String aLog = "";

            String actNum = pReqReceipt.actNum.toLowerCase();

            for (File receiptFile : receiptFiles) {
                String eachName = receiptFile.getName().toLowerCase();
                if (eachName.endsWith(".html") && eachName.contains(actNum)) {
                    aLog = "解析" + receiptFile.getName();
                    WriteBankLogLn(aLog);
                    FGBiz.logTrace.info(getLogCode(), aLog);
                    // 解析HTML文件内容读取回单编号关联关系
                    String receiptHtml = FileHelper.readFileText(receiptFile.getAbsolutePath(), charset);
                    Document receiptDoc = (Document) Jsoup.parse(receiptHtml);

                    Element receiptNoTable = receiptDoc.select("table").last();
                    Elements receiptLinks = receiptNoTable.getElementsByTag("a");

                    int xmlFileCount = 0;

                    for (Element receiptLink : receiptLinks) {
                        totalCount++;
                        String receiptRelativePath = receiptLink.attr("href");
                        String receiptNo = receiptLink.text();
                        String localReceiptPath = pReceiptDir + File.separator + StringHelper.replaceFileSp(receiptRelativePath);

                        File eachRfile = new File(localReceiptPath);
                        if (eachRfile.exists()) {
                            ReceiptData eachData = new ReceiptData();
                            eachData.setBusDate(pOneDay);
                            eachData.istNbr = receiptNo + eachData.busDate;
                            eachData.localFile = pRelativeSaveDir + File.separator + StringHelper.replaceFileSp(receiptRelativePath);
                            eachData.srcFile = receiptRelativePath;
                            eachData.busRef = receiptNo;
                            eachData.receiptNo = receiptNo;
                            eachData.bank = "102";
                            eachData.srcSys = "ICBC02";
                            eachData.ownAct = pReqReceipt.actNum;
                            eachData.downState = "2";
                            eachData.downTime = new Date();

                            String xmlDataPath = localReceiptPath.replace(".pdf", ".xml");
                            File xmldfile = new File(xmlDataPath);
                            if (xmldfile.exists()) {
                                xmlFileCount++;
                                String dataContext = FileHelper.readToString(xmldfile, charsetXmlFile);
                                XmlTextReader dataReader = new XmlTextReader(dataContext);
                                String drcrf = dataReader.getSimpleText("drcrf");
                                if ("1".equals(drcrf)) {
                                    eachData.moneyWay = "1";
                                    eachData.oppActName = dataReader.getSimpleText("craccname");
                                    eachData.oppActNum = dataReader.getSimpleText("craccno");
                                } else if ("2".equals(drcrf)) {
                                    eachData.moneyWay = "2";
                                    eachData.oppActName = dataReader.getSimpleText("draccname");
                                    eachData.oppActNum = dataReader.getSimpleText("draccno");
                                }
                                String amount = dataReader.getSimpleText("amount");
                                if (StringHelper.hasAnyChar(amount)) {
                                    eachData.amount = new BigDecimal(amount);
                                }
                                eachData.summary = dataReader.getSimpleText("f_1");
                            }
                            int inRe = receiptHelper.insertIfNotExistByReceiptNo(eachData, true);
                            if (inRe > 0) {
                                sCount++;
                            } else if (inRe == 0) {
                                existCount++;
                            }
                            WriteBankLogLn(eachData.receiptNo + "写入数据库，影响行数：" + inRe);
                        } else {
                            aLog = "回单编号" + receiptNo + "对应的文件" + localReceiptPath + "不存在";
                            WriteBankLogLn(aLog);
                            FGBiz.logTrace.error(getLogCode(), aLog);
                        }
                    }

                    FGBiz.logTrace.debug(this.getLogCode(), "有效xml文件个数：" + xmlFileCount);

                    FGBiz.logTrace.debug(this.getLogCode(), "解析完成");
                    break;//该账号解析完成
                }
            }

            if (sCount > 0) {
                SyncReceipt.resetDelayCounter();//同步可以开始了
            }
            resp.logMsg = "总遍历" + totalCount + "条，本次新增" + sCount + "条，已存在" + existCount + "条";
            FGBiz.logTrace.info(getLogCode(), resp.logMsg);

        } catch (Exception ex) {
            this.WriteBankLogLn(ex);
        } finally {
            if (receiptHelper != null) {
                receiptHelper.close();
            }
        }
        return resp;
    }


    /**
     * 从ftp下载工行回单压缩包，一天一个压缩文件yyyyMMdd.zip，
     * 下载完之后，解压，因所有账号在一个压缩包内，
     * 为了节省解压次数（每个账号分别查询），判断解压文件正常后，
     * 重命名为正式解压目录
     *
     * @param pBusDate      日期
     * @param pLocalDirPath 下载成功后，解压后目录重命名为该本地目录名
     * @param pLocalZipFile 下载成功后，压缩文件名
     * @throws IOException
     */
    private ExecResponse downZipFile(String pBusDate, String pActNum, String pLocalDirPath, String pLocalZipFile) throws IOException {
        ExecResponse resp = new ExecResponse();

        FtpSet ftpSet = bankFront.getFtpSet("receipt");
        MixFtp mixFtp = null;
        String timePostfix = ReceiptPro.getTransID();
        String localTmpFilePath = pLocalZipFile + "." + timePostfix + ".tmp";
        String tmpDecompressDir = pLocalZipFile + "." + timePostfix + ".unzip";

        try {
            FtpReceiver tmpReceiver = null;
            String downType = bankFront.getStrValueByFor("receiptDownloadType", "");
            FGBiz.logTrace.debug(this.getLogCode(), "receiptDownloadType:" + downType);
            if ("localhost".equalsIgnoreCase(downType)) {//本地目录
                tmpReceiver = new FtpReceiver();
                FGBiz.logTrace.error(this.getLogCode(), "银行返回的回单文件本地目录" + bankFront.bankReceiptMainDir);
                String bankPath = StringHelper.trimEndFileSp(bankFront.bankReceiptMainDir) + File.separator + pBusDate + ".zip";
                File srcBankFile = new File(bankPath);
                if (srcBankFile.exists()) {
                    Date lastMdDate = new Date(srcBankFile.lastModified());
                    double diffSecs = DateHelper.getDifferSecs(new Date(), lastMdDate);
                    if (diffSecs < 10) {//小于10秒的先不传
                        tmpReceiver.connMsg = "回单文件最后修改时间离当前时间小于10秒，暂不获取：" + bankPath;
                        WriteBankLogLn(tmpReceiver.connMsg);
                    } else {
                        File destFile = new File(localTmpFilePath);
                        FileHelper.copyFile(srcBankFile, destFile);
                        if (destFile.exists()) {
                            tmpReceiver.isCompleted = true;
                        } else {
                            tmpReceiver.connMsg = "复制文件失败：复制" + bankPath + "，至" + localTmpFilePath;
                            WriteBankLogLn(tmpReceiver.connMsg);
                        }
                    }
                } else {
                    tmpReceiver.connMsg = "银行回单文件不存在，可能还未推送：" + bankPath;
                    WriteBankLogLn(tmpReceiver.connMsg);
                }
            } else {
                mixFtp = new MixFtp(ftpSet);
                String ftpFileName = pBusDate + ".zip";
                WriteBankLogLn("FTP下载地址: [" + ftpSet.serverIP + ":" + ftpSet.port + "].");
                WriteBankLogLn("开始下载服务器文件- [目录" + ftpSet.downDir + "文件" + ftpFileName + "],至本地临时文件- [" + localTmpFilePath + "].");

                tmpReceiver = mixFtp.downloadFileFromDir(ftpSet.downDir, ftpFileName, localTmpFilePath);
                if (StringHelper.hasAnyChar(tmpReceiver.connMsg)) {
                    WriteBankLogLn(tmpReceiver.connMsg);
                }
            }

            File depressDir = new File(tmpDecompressDir);
            if (tmpReceiver.isCompleted) {
                if (!depressDir.exists()) {
                    depressDir.mkdirs();
                }
                WriteBankLogLn("解压至：[" + tmpDecompressDir + "]");
                ZipHelper.unzipFile(localTmpFilePath, tmpDecompressDir);
                WriteBankLogLn("执行解压结束");
                if (depressDir.exists()) {
                    File[] deFiles = depressDir.listFiles();
                    if (deFiles != null && deFiles.length > 0) {
                        boolean isHtmlChked = false;
                        boolean isPdfChked = false;
                        //校验通过后，需重命名的
                        File needRenameDir = depressDir;

                        File hitDir = depressDir;//临时变量，遍历中赋值
                        for (int i = 0; i < 10; i++) {//存在多层目录，找到有html文件的目录，最多循环10次，即10层目录
                            if (deFiles != null && deFiles.length > 1) {
                                isHtmlChked = FileCheckHelper.existExtFile(deFiles, "html", pActNum);
                                if (isHtmlChked) {
                                    needRenameDir = hitDir;
                                    //校验该文件夹下是否含pdf文件
                                    File pdfFileChk = FileCheckHelper.getFirstDir(deFiles);
                                    isPdfChked = FileCheckHelper.existExtFile(pdfFileChk.listFiles(), "pdf", "");//判断是否存在pdf文件
                                    break;
                                }
                            }
                            if (deFiles != null && deFiles.length > 0) {
                                hitDir = deFiles[0];
                                deFiles = hitDir.listFiles();
                            }
                        }
                        //判断是否包含所查询的账号
                        boolean isExistDir = FileCheckHelper.existDirectory(deFiles, pActNum);
                        if (isHtmlChked == false) {
                            resp.logMsg = "解压后文件夹校验不通过，未找到html文件\r\n";
                        }
                        if (isPdfChked == false) {
                            resp.logMsg += "解压后文件夹校验不通过，未找到pdf文件\r\n";
                        }
                        if (isExistDir == false) {
                            resp.logMsg += "解压后文件夹校验不通过，未找到账号" + pActNum + "的目录\r\n";
                        }
                        if (isHtmlChked && isPdfChked && isExistDir) {//文件校验通过
                            WriteBankLogLn(needRenameDir.getAbsolutePath() + "重命名为" + pLocalDirPath);
                            File destDir = new File(pLocalDirPath);
                            boolean renamed = needRenameDir.renameTo(destDir);
                            if (renamed && destDir.exists()) {//重命名目录成功
                                //重命名zip文件
                                WriteBankLogLn(localTmpFilePath + "重命名为" + pLocalZipFile);
                                File tmpZip = new File(localTmpFilePath);
                                File destZip = new File(pLocalZipFile);
                                boolean rezip = tmpZip.renameTo(destZip);
                                if (rezip && destZip.exists()) {
                                    resp.isSucceed = true;
                                } else {
                                    resp.logMsg = "重命名失败：" + localTmpFilePath + "重命名为" + pLocalZipFile;
                                }
                            } else {
                                resp.logMsg = "重命名失败：" + needRenameDir.getName() + "重命名为" + pLocalDirPath;
                            }
                        }
                    } else {
                        resp.logMsg = "解压后文件夹" + tmpDecompressDir + "下为空";
                    }
                } else {
                    resp.logMsg = "解压目标文件夹" + tmpDecompressDir + "不存在，创建失败";
                }
            } else {
                resp.logMsg = tmpReceiver.connMsg;
            }
        } catch (Exception ex) {
            WriteBankLogLn(ex);
            resp.logMsg = LogHelper.getStackTrace(ex);
        } finally {
            if (mixFtp != null) {
                mixFtp.disconnect();
            }
            try {
                WriteBankLogLn("删除临时目录：" + tmpDecompressDir);
                FileHelper.deleteRecursive(tmpDecompressDir);
            } catch (Exception e) {
                WriteBankLogLn(e);
            }
            try {
                WriteBankLogLn("删除临时文件：" + localTmpFilePath);
                FileHelper.deleteFileIfExists(localTmpFilePath);
            } catch (Exception e) {
                WriteBankLogLn(e);
            }
        }
        return resp;
    }


}
